class Solution {
public:
    void _numIslands(vector<vector<char>>& grid, int row, int col)
    {
        if ((row < 0 || row >= grid.size()) || (col < 0 || col >= grid[0].size()))
            return;
        if (grid[row][col] == '0')
            return;
        grid[row][col] = '0';
        _numIslands(grid, row - 1, col);
        _numIslands(grid, row + 1, col);
        _numIslands(grid, row, col - 1);
        _numIslands(grid, row, col + 1);
    }

    int numIslands(vector<vector<char>>& grid) {
        int ret = 0;
        for (int i = 0; i < grid.size(); i++)
        {
            for (int j = 0; j < grid[0].size(); j++)
            {
                if (grid[i][j] == '1')
                {
                    ret++;
                    _numIslands(grid, i, j);
                }
            }
        }
        return ret;
    }
};